/**
 * Copyright 2014,Peak Tai 台俊峰(taijunfeng_it@sina.com).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * 		http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.cenluan.tweet.comment;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;

import com.cenluan.common.Const;
import com.cenluan.ext.jfinal.PkController;
import com.cenluan.kit.UUIDKit;
import com.cenluan.kit.XssKit;
import com.cenluan.me.call.Call;
import com.cenluan.me.message.Message;
import com.cenluan.tweet.Tweet;
import com.cenluan.user.User;
import com.cenluan.user.UserKit;
import com.cenluan.user.log.UserLog;
import com.jfinal.aop.Before;

/**
 * 微博评论
 * 
 * @author peak
 * 
 */
public class TweetCommentController extends PkController {
	/**
	 * 保存
	 */
	@Before(TweetCommentValidator.class)
	public void save() {
		User user = getSessionAttr(Const.SESSION_USER);
		Tweet tweet = Tweet.dao.findById(getPara("tweetId"));
		TweetComment comment = new TweetComment();
		comment.set("tweetId", getPara("tweetId"));
		// 内容转义并解析引用
		String content = getPara("content");
		content = XssKit.escapeStr(content);
		Set<String> referers = new HashSet<String>();
		content = UserKit.generateRefererLinks(content, referers);
		comment.set("content", content);
		// 设置其它属性，保存
		comment.set("createrId", user.getStr("id"));
		comment.set("createTime", new Date());
		comment.set("id", UUIDKit.generateUUID());
		comment.save();
		// @通知
		for (String to : referers) {
			Call.save(user.getStr("id"), to, Call.TYPE_TWEET_COMMENT_AT,
					comment.getStr("id"));
		}
		if (!tweet.getStr("createrId").equals(user.getStr("id"))) {
			Call.save(user.getStr("id"), tweet.getStr("createrId"),
					Call.TYPE_POST_TWEET_COMMENT, comment.getStr("id"));
		}
		// 动态，如果用户勾选了同步在自己的动态里
		if (!isParaBlank("showLog")) {
			UserLog.save(user.getStr("id"), UserLog.TYPE_TWEET_COMMENT,
					comment.getStr("id"));
		}
		setAttr("ok", true);
		setAttr("id", comment.getStr("id"));
		renderJson();
	}

	/**
	 * 展示，返回一段html代码
	 */
	public void show() {
		TweetComment comment = TweetComment.dao.findById(getPara());
		setAttr("comment", comment);
	}

	/**
	 * 删除
	 */
	public void delete() {
		User user = getSessionAttr(Const.SESSION_USER);
		TweetComment comment = TweetComment.dao.findById(getPara());
		Tweet tweet = comment.getTweet();
		String userId = user.getStr("id");
		if (!user.isAdmin() && !userId.equals(tweet.getStr("createrId"))
				&& !userId.equals(comment.getStr("createrId"))) {
			setAttr("msg", "您无权限删除该评论");
			renderJson();
			return;
		}
		comment.delete();
		// 通知
		if (!userId.equals(comment.getStr("createrId"))) {
			String content = "删除了您在动态里的评论："
					+ Jsoup.clean(comment.getStr("content"), Whitelist.none());
			Message.save(userId, comment.getStr("createrId"), content);
		}
		setAttr("ok", true);
		renderJson();
	}
}
